//app.js
var Utils = require('/utils/util.js');
App({
    globalData:{
        host:Utils.Host,
        Rule:Utils.Rule,
        appid: Utils.FootConvenientService.appid,
        path: Utils.FootConvenientService.path,
        envVersion: Utils.FootConvenientService.envVersion,
        orderType:14,
        bizDoingFlag:"1",
        bizFinishFlag:"2",
        bizAllFlag:"3",
        userInfo:null,
        openId:"",
        cacheData: "CACHE_FORM_DATA",
        cacheOrderPay: 'CACHE_FORM_PAY',

        cacheAddressKey:"CACHE_ADDRESS_KEY",
        cachePageKey:"CACHE_PAGE_KEY",
        cacheOrderResKey:"CACHE_ORDER_RES_KEY",
        cacheWxOpenIdKey:"CACHE_WX_OPENID_KEY"
    },
    onLaunch: function () {
        this.getUserInfo();
    },

    //跨小程序跳转
    crossToUrl:function (res) {
        if (wx.navigateToMiniProgram) {
          wx.navigateToMiniProgram({
            appId: res.appid,
            path: res.path,
            envVersion: res.envVersion,
            success:function(data) {
              console.log('跳转成功',data)
            },
            fail:function(data){
              console.log('跳转失败',data)
            }
          })
        } else {
          // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
          wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
          })
        }
    },
    toastAuthFailModal:function () {
        wx.showModal({
            title: '微信认证失败',
            content: '请先在小程序列表完全删除“邮证机动车牌”，然后通过扫描二维码或在小程序中搜索“邮证机动车牌”再次进入，重新获取微信认证。',
            showCancel: false,
            success: function (res) {
                if (res.confirm) {
                    console.log('用户点击确定');
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        });
    },
    getUserInfo: function (cb) {
        var that = this;
        if (this.globalData.userInfo) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            wx.login({//调用登录接口
                success: function (res) {
                    wx.getUserInfo({
                        success: function (result) {
                            that.globalData.userInfo = result.userInfo;
                            that.globalData.userInfo['code'] = res.code;
                            var opt = {///授权成功，更新用户信息
                                url: that.globalData.host + "/wx/cn/uwu.ajax",
                                data:that.globalData.userInfo,
                                sucFn: function (data) {
                                }
                            };
                            that.ajax(opt);
                            typeof cb == "function" && cb(that.globalData.userInfo);
                        },
                        fail:function (result) {
                            // var opt = {//需要查询数据库是否有记录该用户的信息，有就把头像和名称拿出来，没有就用默认的头像
                            //     url: that.globalData.host + "/wx/cn/gwu.ajax",
                            //     data: {
                            //         'code': res.code
                            //     },
                            //     sucFn: function (data) {
                            //         that.globalData.userInfo= data.body;
                            //     }
                            // };
                            // that.ajax(opt);
                            var userObj = {
                                "avatarUrl": "https://yzres.oss-cn-shenzhen.aliyuncs.com/wx/images/common/mine_default_heard@2x.png",
                                "nickName": "游客"
                            };
                            that.globalData.userInfo = userObj;
                            wx.showModal({
                                title: '微信授权失败',
                                content: '请先在小程序列表完全删除“邮证机动车牌”，然后通过扫描二维码或在小程序中搜索“邮证机动车牌”再次进入，允许小程序获取您的用户信息。',
                                showCancel:false,
                                success: function(res) {
                                    if (res.confirm) {
                                        console.log('用户点击确定');
                                    } else if (res.cancel) {
                                        console.log('用户点击取消');
                                    }
                                }
                            })
                        }
                    })
                }
            })
        }
    },
    makePhoneCall:function (phone) {
        wx.makePhoneCall({
            phoneNumber: phone //仅为示例，并非真实的电话号码
        });
    },
    toYMDHMSDate:function (number) {
       return Utils.toYMDHMSDate(number);
    },
    toYMDDate:function (number) {
       return Utils.toYMDDate(number);
    },
    toast:function (mes,icon) {
        Utils.toast(mes, icon);
    },
    loadToast:function (time) {
        var duration = (time == null || time == "" || typeof(time) == "undefined") ? 60000 : time;
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration:duration,
            mask:true
        })
    },
    hideToast:function() {
        wx.hideToast()
    },
    ajax:function (opt) {
        opt['pageObj'] = this.getCurrentPage();
        opt.data['WX_OPENID'] = this.globalData.openId;
        Utils.ajax(opt);
    },
    errorToast:function(mes) {
        Utils.errorToast(this.getCurrentPage(), mes);
    },
    hidePopErrorMes:function() {
        Utils.hidePopErrorMes(this.getCurrentPage());
    },
    check:function(jsonArray) {
        return Utils.check(jsonArray,this.getCurrentPage());
    },
    initAddress:function (){
        Utils.initAddress(this.getCurrentPage());
    },
    bindAddressChange:function (e){
        Utils.bindAddressChange(e,this.getCurrentPage());
    },
    /**封装微信的方法,统一入口，方便后续做统一的处理**/
    navigateTo:function (data) {
        wx.navigateTo({
          url: data.url,
          success: function(res){
              if (!(data.success==null || data.success=="" || typeof(data.success)=="undefined")){
                  data.success(res);
              }
          },
          fail: function() {
              if (!(data.fail==null || data.fail=="" || typeof(data.fail)=="undefined")){
                  data.fail(res);
              }
          },
          complete: function() {
              if (!(data.complete==null || data.complete=="" || typeof(data.complete)=="undefined")){
                  data.complete(res);
              }
          }
        })
    },
    redirectTo:function (data) {
        wx.redirectTo({
        url: data.url,
        success: function(res){
            if (!(data.success==null || data.success=="" || typeof(data.success)=="undefined")){
                data.success(res);
            }
        },
        fail: function() {
            if (!(data.fail==null || data.fail=="" || typeof(data.fail)=="undefined")){
                data.fail(res);
            }
        },
        complete: function() {
            if (!(data.complete==null || data.complete=="" || typeof(data.complete)=="undefined")){
                 data.complete(res);
            }
        }
        })
    },
    getStorageSync:function (key) {
        return wx.getStorageSync(key);
    },
    setStorageSync:function (key,value) {
         wx.setStorageSync(key,value);
    },
    removeStorageSync:function (key) {
        try {
            wx.removeStorageSync(key)
        } catch (e) {
            // Do something when catch error
        }
    },
    removeStorage:function (key) {
        wx.removeStorage({key: key, success: function(res) {} });
    },
    isAndroid:function () {
        var res = wx.getSystemInfoSync()
        if(res.model.indexOf('iPhone')>-1){
          return false;
        }else {
          return true;
        }
    }
});